home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / mosmllib / test / real.sml < prev    next >
Encoding:
Text File  |  1997-08-18  |  9.8 KB  |  320 lines  |  [TEXT/R*ch]

  1. (* test/real.sml -- PS 1995-03-24, 1996-05-16, 1996-07-02, 1996-09-25 *)
  2.  
  3. use "auxil.sml";
  4.  
  5. local 
  6.     open Real
  7. in    
  8.  
  9. val test1 = check(sign ~57.0 = ~1 andalso sign 99.0 = 1 andalso sign 0.0 = 0);
  10. val test2 = check(sameSign(~255.0, ~256.0) andalso sameSign(255.0, 256.0) 
  11.           andalso sameSign(0.0, 0.0));
  12. val test3 = check(sign 1E~300 = 1 andalso sign ~1E~300 = ~1
  13.           andalso sign 1E300 = 1 andalso sign ~1E300 = ~1);
  14.  
  15. local 
  16.     val args = [0.0, 99.0, ~5.0, 1.1, ~1.1, 1.9, ~1.9, 2.5, ~2.5, 
  17.         1000001.4999, ~1000001.4999];
  18.     val minInt = valOf Int.minInt;
  19.     val maxInt = valOf Int.maxInt;
  20.     val rminInt = real minInt;
  21.     val rmaxInt = real maxInt;
  22.     fun chkminmax f min max = 
  23.     check'(fn _ => 
  24.            List.all (fn r => f r = minInt) (rminInt :: min)
  25.            andalso List.all (fn r => f r = maxInt) (rmaxInt :: max));
  26.     fun chkfail f r =
  27.     (f r; "WRONG") 
  28.     handle Overflow => "OK" | _ => "WRONG"
  29. in
  30. val test4a = check(map ceil args  
  31.            = [0, 99, ~5, 2, ~1, 2, ~1, 3, ~2, 1000002, ~1000001]);
  32. val test4b = chkminmax ceil [rminInt-0.9] [rmaxInt-0.1];
  33. val test4c = map (chkfail ceil) [rminInt-1.0, rmaxInt+0.1];
  34.  
  35. val test5a = check(map floor args 
  36.            = [0, 99, ~5, 1, ~2, 1, ~2, 2, ~3, 1000001, ~1000002]);
  37. val test5b = chkminmax floor [rminInt+0.1] [rmaxInt+0.9];
  38. val test5c = map (chkfail floor) [rminInt-0.1, rmaxInt+1.0];
  39.  
  40. val test6a = check(map trunc args 
  41.            = [0, 99, ~5, 1, ~1, 1, ~1, 2, ~2, 1000001, ~1000001]);
  42. val test6b = chkminmax trunc [rminInt-0.9] [rmaxInt+0.9];
  43. val test6c = map (chkfail trunc) [rminInt-1.0, rmaxInt+1.0];
  44.  
  45. val test7a = check(map round args 
  46.            = [0, 99, ~5, 1, ~1, 2, ~2, 2, ~2, 1000001, ~1000001]);
  47. val test7b = chkminmax round [rminInt-0.5, rmaxInt+0.4]
  48. val test7c = map (chkfail round) [rminInt-0.6, rmaxInt+0.5];
  49.  
  50. end
  51.  
  52. val test8 = check(0.0 = real 0 andalso 2.0 = real 2 andalso ~2.0 = real ~2);
  53.  
  54. fun chk(s, r) = 
  55.     let val eps = abs r * 1E~10 
  56.     in 
  57.     check'(fn _ => 
  58.            case fromString s of
  59.            SOME res => abs(res - r) <= eps
  60.                  | NONE     => false)
  61.     end
  62.  
  63. val test9a = 
  64.     List.map chk[("12.", 12.0),
  65.          ("12.E", 12.0),
  66.          ("12.E+", 12.0),
  67.          ("12.E-", 12.0),
  68.          ("12.E2", 12.0),
  69.          ("12.E+2", 12.0),
  70.          ("12.E-2", 12.0),
  71.          ("12E+", 12.0),
  72.          ("12E-", 12.0)];
  73.  
  74. val test9b = 
  75.     List.map chk[("0", 0.0),
  76.          ("156", 156.0),
  77.          ("+156", 156.0), 
  78.          ("~156", ~156.0), 
  79.          ("-156", ~156.0), 
  80.          ("156.25", 156.25), 
  81.          ("+156.25", 156.25), 
  82.          ("~156.25", ~156.25), 
  83.          ("-156.25", ~156.25),
  84.          (".25", 0.25),
  85.          ("+.25", 0.25),
  86.          ("~.25", ~0.25),
  87.          ("-.25", ~0.25)];
  88.  
  89. val test9c = 
  90.     List.map chk[ ("156E024", 156E024),
  91.           ("+156E024", 156E024),
  92.           ("~156E024", ~156E024),
  93.           ("-156E024", ~156E024),
  94.           ("156.25E024", 156.25E024),
  95.           ("+156.25E024", 156.25E024),
  96.           ("~156.25E024", ~156.25E024),
  97.           ("-156.25E024", ~156.25E024),
  98.           (".25E024", 0.25E024),
  99.           ("+.25E024", 0.25E024),
  100.           ("~.25E024", ~0.25E024),
  101.           ("-.25E024", ~0.25E024)];
  102.  
  103. val test9d = 
  104.     List.map chk[ ("156E+024", 156E024),
  105.           ("+156E+024", 156E024),
  106.           ("~156E+024", ~156E024),
  107.           ("-156E+024", ~156E024),
  108.           ("156.25E+024", 156.25E024),
  109.           ("+156.25E+024", 156.25E024),
  110.           ("~156.25E+024", ~156.25E024),
  111.           ("-156.25E+024", ~156.25E024),
  112.           (".25E+024", 0.25E024),
  113.           ("+.25E+024", 0.25E024),
  114.           ("~.25E+024", ~0.25E024),
  115.           ("-.25E+024", ~0.25E024)];
  116.  
  117. val test9e = 
  118.     List.map chk[ ("156E~024", 156E~024),
  119.           ("+156E~024", 156E~024),
  120.           ("~156E~024", ~156E~024),
  121.           ("-156E~024", ~156E~024),
  122.           ("156.25E~024", 156.25E~024),
  123.           ("+156.25E~024", 156.25E~024),
  124.           ("~156.25E~024", ~156.25E~024),
  125.           ("-156.25E~024", ~156.25E~024),
  126.           (".25E~024", 0.25E~024),
  127.           ("+.25E~024", 0.25E~024),
  128.           ("~.25E~024", ~0.25E~024),
  129.           ("-.25E~024", ~0.25E~024)];
  130.  
  131. val test9f = 
  132.     List.map chk[ ("156E-024", 156E~024),
  133.           ("+156E-024", 156E~024),
  134.           ("~156E-024", ~156E~024),
  135.           ("-156E-024", ~156E~024),
  136.           ("156.25E-024", 156.25E~024),
  137.           ("+156.25E-024", 156.25E~024),
  138.           ("~156.25E-024", ~156.25E~024),
  139.           ("-156.25E-024", ~156.25E~024),
  140.           (".25E-024", 0.25E~024),
  141.           ("+.25E-024", 0.25E~024),
  142.           ("~.25E-024", ~0.25E~024),
  143.           ("-.25E-024", ~0.25E~024)];
  144.  
  145. val test9g = 
  146.     List.map chk[ ("156e024", 156E024),
  147.           ("+156e024", 156E024),
  148.           ("~156e024", ~156E024),
  149.           ("-156e024", ~156E024),
  150.           ("156.25e024", 156.25E024),
  151.           ("+156.25e024", 156.25E024),
  152.           ("~156.25e024", ~156.25E024),
  153.           ("-156.25e024", ~156.25E024),
  154.           (".25e024", 0.25E024),
  155.           ("+.25e024", 0.25E024),
  156.           ("~.25e024", ~0.25E024),
  157.           ("-.25e024", ~0.25E024)];
  158.  
  159. val test9h = 
  160.     List.map chk[ ("156e+024", 156E024),
  161.           ("+156e+024", 156E024),
  162.           ("~156e+024", ~156E024),
  163.           ("-156e+024", ~156E024),
  164.           ("156.25e+024", 156.25E024),
  165.           ("+156.25e+024", 156.25E024),
  166.           ("~156.25e+024", ~156.25E024),
  167.           ("-156.25e+024", ~156.25E024),
  168.           (".25e+024", 0.25E024),
  169.           ("+.25e+024", 0.25E024),
  170.           ("~.25e+024", ~0.25E024),
  171.           ("-.25e+024", ~0.25E024)];
  172.  
  173. val test9i = 
  174.     List.map chk[ ("156e~024", 156E~024),
  175.           ("+156e~024", 156E~024),
  176.           ("~156e~024", ~156E~024),
  177.           ("-156e~024", ~156E~024),
  178.           ("156.25e~024", 156.25E~024),
  179.           ("+156.25e~024", 156.25E~024),
  180.           ("~156.25e~024", ~156.25E~024),
  181.           ("-156.25e~024", ~156.25E~024),
  182.           (".25e~024", 0.25E~024),
  183.           ("+.25e~024", 0.25E~024),
  184.           ("~.25e~024", ~0.25E~024),
  185.           ("-.25e~024", ~0.25E~024)];
  186.  
  187. val test9j = 
  188.     List.map chk[ ("156e-024", 156E~024),
  189.           ("+156e-024", 156E~024),
  190.           ("~156e-024", ~156E~024),
  191.           ("-156e-024", ~156E~024),
  192.           ("156.25e-024", 156.25E~024),
  193.           ("+156.25e-024", 156.25E~024),
  194.           ("~156.25e-024", ~156.25E~024),
  195.           ("-156.25e-024", ~156.25E~024),
  196.           (".25e-024", 0.25E~024),
  197.           ("+.25e-024", 0.25E~024),
  198.           ("~.25e-024", ~0.25E~024),
  199.           ("-.25e-024", ~0.25E~024)];
  200.  
  201. val test10 = 
  202.     List.map (fn s => case fromString s of NONE => "OK" | _ => "WRONG") 
  203.              ["e10", "E10", 
  204.           "+e10", "+E10", 
  205.           "~e10", "~E10", 
  206.           "-e10", "-E10"];
  207.  
  208. (* Note: There is some unclarity concerning rounding.  Should 1.45,
  209. rounded to two significant digits, be "1.4" (nearest even digit) or
  210. "1.5" (new greater digit) in case of a tie?  PS 1996-05-16 *)
  211.  
  212. val test11a = 
  213.     (fmt (StringCvt.FIX (SOME ~1)) 12.3456; "WRONG")
  214.     handle Size => "OK" | _ => "WRONG";
  215.  
  216. val test11b = 
  217.     (fmt (StringCvt.FIX (SOME 100000)) 12.3456)
  218.     handle Size => "OK" | _ => "WRONG";
  219.  
  220. fun chkFIX (r, s0, s1, s2, s6) = 
  221.     fmt (StringCvt.FIX (SOME 0)) r = s0
  222.     andalso fmt (StringCvt.FIX (SOME 1)) r = s1
  223.     andalso fmt (StringCvt.FIX (SOME 2)) r = s2
  224.     andalso fmt (StringCvt.FIX (SOME 6)) r = s6
  225.     andalso fmt (StringCvt.FIX NONE) r = s6;
  226.  
  227. fun chkFIX' (r, s0, s1, s2, s6) = 
  228.     chkFIX(r, s0, s1, s2, s6) 
  229.     andalso (r = 0.0 orelse chkFIX(~r, "~"^s0, "~"^s1, "~"^s2, "~"^s6));
  230.  
  231. val test11c = 
  232.     check'(fn _ =>
  233.        List.all chkFIX'
  234.        [(0.0, "0", "0.0", "0.00", "0.000000"),
  235.         (1.0, "1", "1.0", "1.00", "1.000000"),
  236.         (1.4, "1", "1.4", "1.40", "1.400000"),
  237.         (1.5, "2", "1.5", "1.50", "1.500000"),
  238. (* dubious  (2.5, "2", "2.5", "2.50", "2.500000"), *)
  239.         (1.6, "2", "1.6", "1.60", "1.600000"),
  240.             (1.45, "1", "1.4", "1.45", "1.450000"),
  241.         (3.141592653589, "3", "3.1", "3.14", "3.141593"),
  242.         (91827364509182.0, "91827364509182", "91827364509182.0", 
  243.          "91827364509182.00", "91827364509182.000000")]);
  244.  
  245. val test12a = 
  246.     (fmt (StringCvt.SCI (SOME ~1)) 12.3456; "WRONG")
  247.     handle Size => "OK" | _ => "WRONG";
  248.  
  249. val test12b = 
  250.     (fmt (StringCvt.SCI (SOME 100000)) 12.3456)
  251.     handle Size => "OK" | _ => "WRONG";
  252.  
  253. fun chkSCI (r, s0, s1, s2, s6) = 
  254.     fmt (StringCvt.SCI (SOME 0)) r = s0
  255.     andalso fmt (StringCvt.SCI (SOME 1)) r = s1
  256.     andalso fmt (StringCvt.SCI (SOME 2)) r = s2
  257.     andalso fmt (StringCvt.SCI (SOME 6)) r = s6
  258.     andalso fmt (StringCvt.SCI NONE) r = s6;
  259.  
  260. fun chkSCI' (r, s0, s1, s2, s6) = 
  261.     chkSCI(r, s0, s1, s2, s6) 
  262.     andalso (r = 0.0 orelse chkSCI(~r, "~"^s0, "~"^s1, "~"^s2, "~"^s6));
  263.  
  264. val test12c = 
  265.     check'(fn _ => 
  266.        List.all chkSCI'
  267.        [(0.0, "0E00", "0.0E00", "0.00E00", "0.000000E00"),
  268.         (0.0012345678, "1E~03", "1.2E~03", "1.23E~03", "1.234568E~03"),
  269.         (1.0, "1E00", "1.0E00", "1.00E00", "1.000000E00"),
  270.         (1.4, "1E00", "1.4E00", "1.40E00", "1.400000E00"),
  271.         (1.5, "2E00", "1.5E00", "1.50E00", "1.500000E00"),
  272. (* dubious  (2.5, "2E00", "2.5E00", "2.50E00", "2.500000E00"), *)
  273.         (1.6, "2E00", "1.6E00", "1.60E00", "1.600000E00"),
  274.             (1.45, "1E00", "1.4E00", "1.45E00", "1.450000E00"),
  275.         (3.141592653589, "3E00", "3.1E00", "3.14E00", "3.141593E00"),
  276.         (91827364509182.0, "9E13", "9.2E13", "9.18E13", "9.182736E13")]);
  277.  
  278. val test13a = 
  279.     (fmt (StringCvt.GEN (SOME 0)) 12.3456; "WRONG")
  280.     handle Size => "OK" | _ => "WRONG";
  281.  
  282. val test13b = 
  283.     (fmt (StringCvt.GEN (SOME 100000)) 12.3456)
  284.     handle Size => "OK" | _ => "WRONG";
  285.  
  286. fun chkGEN (r, s1, s2, s6, s12) = 
  287.     fmt (StringCvt.GEN (SOME 1)) r = s1
  288.     andalso fmt (StringCvt.GEN (SOME 2)) r = s2
  289.     andalso fmt (StringCvt.GEN (SOME 6)) r = s6
  290.     andalso fmt (StringCvt.GEN (SOME 12)) r = s12
  291.     andalso fmt (StringCvt.GEN NONE) r = s12
  292.     andalso toString r = s12;
  293.  
  294. fun chkGEN' (r, s1, s2, s6, s12) = 
  295.     chkGEN(r, s1, s2, s6, s12) 
  296.     andalso (r = 0.0 orelse 
  297.          chkGEN(~r, "~"^s1, "~"^s2, "~"^s6, "~"^s12));
  298.  
  299. val test13c = 
  300.     check'(fn _ =>
  301.        List.all chkGEN'
  302.        [(0.0,               "0.0", "0.0",     "0.0", "0.0"),
  303.         (0.0012345678,    "0.001", "0.0012", "0.00123457", 
  304.          "0.0012345678"),
  305.         (1.0,              "1.0", "1.0",  "1.0", "1.0"),
  306.         (1.4,              "1.0", "1.4",  "1.4", "1.4"),
  307.         (1.5,              "2.0", "1.5",  "1.5", "1.5"),
  308. (* dubious  (2.5,              "2.0", "2.5",  "2.5", "2.5"), *)
  309.         (1.6,              "2.0", "1.6",  "1.6", "1.6"),
  310.             (1.45,             "1.0", "1.4",  "1.45", "1.45"),
  311.         (3.141592653589,   "3.0", "3.1",  "3.14159", "3.14159265359"),
  312.         (91827364509182.0, "9E13", "9.2E13",  "9.18274E13", 
  313.                             "9.18273645092E13")]);
  314. end
  315.  
  316. (* 
  317. fun f r n = Real.fmt (StringCvt.GEN (SOME n)) r;
  318. fun ff r = map (f r) [1,2,6,12];
  319.  *)
  320.